package study.datastructure;/**
 * @program: datastructure
 * @author: lcy
 * @create: 2025-02-11 21:04
 */

/**
 2025/2/11,
 */


public class ThreadLocalExample {
    private static final ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        Runnable task = () -> {
            // 设置线程本地变量
            threadLocal.set(Thread.currentThread().getName());
            // 获取线程本地变量
            System.out.println(Thread.currentThread().getName() + ": " + threadLocal.get());
            // 移除线程本地变量
            threadLocal.remove();
        };

        Thread thread1 = new Thread(task, "Thread-1");
        Thread thread2 = new Thread(task, "Thread-2");

        thread1.start();
        thread2.start();
    }
}